
package com.dhcc.bpm.modules.api.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Enumeration;

@Slf4j
@RestController
@RequestMapping("/wsdl")
public class WsdlController {
    @Value("${bpm.file-public-path}")
    private String filePath;
    @GetMapping(value = "/api/{productId}/{txCode}",produces = MediaType.APPLICATION_XML_VALUE)
    public String get(HttpServletRequest servletRequest, @PathVariable("productId") String productId,@PathVariable("txCode") String txCode) throws IOException {
        Enumeration<String> enumeration =  servletRequest.getParameterNames();
        boolean flag = false;
        if(enumeration.hasMoreElements()){
            if("wsdl".equals(enumeration.nextElement())){
                flag = true;
            }
        }
        if(!flag){
            return "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                    "\t<soap:Body>\n" +
                    "\t\t<soap:Fault>\n" +
                    "\t\t\t<faultcode>soap:Server</faultcode>\n" +
                    "\t\t\t<faultstring>No binding operation info while invoking unknown method with params unknown.</faultstring>\n" +
                    "\t\t</soap:Fault>\n" +
                    "\t</soap:Body>\n" +
                    "</soap:Envelope>";
        }
        String s = "";
        String basePath = filePath.substring(0,filePath.length()-1)+ File.separator+"wsdl"+File.separator+productId+File.separator+txCode+".wsdl";
        InputStreamReader in = new InputStreamReader(new FileInputStream(basePath),"UTF-8");
        BufferedReader br = new BufferedReader(in);
        StringBuffer content = new StringBuffer();
        while ((s=br.readLine())!=null){
            content = content.append(s);
        }
        return content.toString();
    }


    @GetMapping(value = "/api/{apiCode}",produces = MediaType.APPLICATION_XML_VALUE)
    public String get(HttpServletRequest servletRequest, @PathVariable("apiCode") String apiCode) throws IOException {
        Enumeration<String> enumeration =  servletRequest.getParameterNames();
        boolean flag = false;
        if(enumeration.hasMoreElements()){
            if("wsdl".equals(enumeration.nextElement())){
                flag = true;
            }
        }
        if(!flag){
            return "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                    "\t<soap:Body>\n" +
                    "\t\t<soap:Fault>\n" +
                    "\t\t\t<faultcode>soap:Server</faultcode>\n" +
                    "\t\t\t<faultstring>No binding operation info while invoking unknown method with params unknown.</faultstring>\n" +
                    "\t\t</soap:Fault>\n" +
                    "\t</soap:Body>\n" +
                    "</soap:Envelope>";
        }
        String s = "";
        String basePath = filePath.substring(0,filePath.length()-1)+ File.separator+"wsdl"+File.separator+"api"+File.separator+apiCode+".wsdl";
        InputStreamReader in = new InputStreamReader(new FileInputStream(basePath),"UTF-8");
        BufferedReader br = new BufferedReader(in);
        StringBuffer content = new StringBuffer();
        while ((s=br.readLine())!=null){
            content = content.append(s);
        }
        return content.toString();
    }
}

